package com.baidu.infinity.ui.fragment

import android.animation.Animator
import android.os.Bundle
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import com.baidu.infinity.R
import com.baidu.infinity.databinding.FragmentWelcomeBinding
import com.baidu.infinity.ui.base.BaseFragment
import com.baidu.infinity.ui.util.PasswordType
import com.baidu.infinity.viewmodel.UserViewModel
import java.util.Date

class WelcomeFragment : BaseFragment<FragmentWelcomeBinding>(){
    val userViewModel: UserViewModel by activityViewModels()
    private var isFindFinihsed = false
    override fun initUI(savedInstanceState: Bundle?) {
        super.initUI(savedInstanceState)
        userViewModel.isFindFinihsed.observe(this){
            isFindFinihsed = it
        }
        mBinding.lottieView.addAnimatorListener(object : Animator.AnimatorListener{
            override fun onAnimationStart(animation: Animator) {

            }

            override fun onAnimationEnd(animation: Animator) {
                if(isFindFinihsed)navitate()
            }

            override fun onAnimationCancel(animation: Animator) {

            }

            override fun onAnimationRepeat(animation: Animator) {

            }

        })
    }
    private fun navitate(){
        val user = userViewModel.currentUser
        if(user != null){
            val time = Date().time - user.loginDate.time
            if(time > user.vaildate){
                if(user.passwordType == 0){
                    findNavController().navigate(R.id.action_welcomeFragment_to_pinLoginFragment)
                }else{
                    findNavController().navigate(R.id.action_welcomeFragment_to_picLoginFragment)
                }
            }else{
                findNavController().navigate(R.id.action_welcomeFragment_to_homeFragment)
            }
        }else{
            findNavController().navigate(R.id.action_welcomeFragment_to_homeFragment)
        }

    }
}